<?php
namespace App\Http\Controllers\Backend\Sysconf;

use App\Exceptions\CommonException;
use App\Http\Controllers\Backend\BaseController;
use App\Http\Requests\Backend\Sysconf\AreasRequest;
use App\Repositories\PrtAreasRepository;
use Illuminate\Http\Request;

/**
 * 省市区地址库相关操作
 * @author:yanxs<541139655@qq.com>
 * @version: 1.0
 * @date:2021/7/13
 */
class AreasController extends BaseController
{
    protected $viewPath = 'backend.sysconf.areas';  //当前控制器所的view所在的目录
    protected $modules = 'sys';        //当前控制器所属模块
    protected $codePrefix = '';        //编码前缀
    public function __construct(PrtAreasRepository $Repository)
    {
        parent::__construct();
        $this->repositories = $Repository;
    }
    public function table(Request $request)
    {
        $inputs = $request->all();

        if (empty( $inputs['pid'])) {
            $inputs['level'] = 1;
        }
        $list = $this->repositories->getRows($inputs,'area_id', 'asc')->toArray();
        $htmlContents = $this->renderHtml('',['list' =>$list]);
        if (!empty( $inputs['pid']) && empty($list)) {
            $htmlContents = '';
        }
        return $this->jsonSuccess(['html' => $htmlContents]);
        //return parent::table($request); // TODO: Change the autogenerated stub
    }
    /**
     * 通用表单展示
     * @param Request $request
     * @return mixed
     */
    protected function form(Request $request)
    {
        try {
            if($request->ajax())
            {
                $areaId =  $request->input('area_id');
                if (!empty($areaId)) {
                    $row = $this->repositories->getRow(['area_id' =>$areaId]);
                } else {
                    $row = [];
                }

                $htmlContents = $this->renderHtml($this->viewPath.'.'.$this->form, ['row' => $row]);
                return $this->jsonSuccess(['html' => $htmlContents]);
            }else{
                return view("admin.tips");
            }
        } catch (CommonException $e) {

            return $this->jsonFailed($e->getMessage());
        }

    }

    //添加/编辑操作
    public function save(AreasRequest $request)
    {
        $params = $request->all();

        $data['area_name']   =  $params['area_name'];
        $data['area_code']   =  $params['area_code'];
        $data['pid']         =  $params['city'] >0 ? $params['city'] :  $params['province'];

        if ($params['province'] <0 ) {
            $level = 1;
        } else {
            $level = $params['city'] >0 ? 3 : 2;
        }

        $data['level']       =  $level;
        $data['longitude']   =  $params['longitude'];
        $data['latitude']    =  $params['latitude'];

        $ret = $this->repositories->save($data);
        if ($ret) {
            return $this->jsonSuccess([]);
        } else {
            return $this->jsonFailed('');
        }
    }

    /**
     * 删除记录(软删除)
     * @param Request $request
     * @return bool
     */
    protected function delete(Request $request)
    {
        $ret = $this->repositories->delete($request->id);
        if($ret) {
            return $this->jsonSuccess(['']);
        } else {
            return $this->jsonFailed( "删除记录失败");
        }
    }

}
